// vue-router 导出
import Router from 'vue-router';
import { store } from '@/store/trade';
let { state } = store;

import Main from '@component/trade/main.vue';
// 企业管理
import Info from '@component/trade/enterprise/Info.vue';
import Message from '@component/trade/enterprise/Message.vue';
import Judge from '@component/trade/enterprise/judge.vue';
import DraftBeans from '@component/trade/enterprise/draftBeans.vue';
import Safe from '@component/trade/enterprise/safe.vue';
import Operater from '@/component/trade/enterprise/operater.vue';

//银行速贴
import Publish from '@component/trade/discount/publish.vue';
import DiscountOrder from '@component/trade/discount/order.vue';
import DiscountOrderDetail from '@component/trade/discount/DiscountOrderDetail.vue';

// 我是买方
// import Buyer from '@component/trade/buyer/index.vue'
import MyBidding from '@component/trade/buyer/myBidding/index.vue';
import MyBiddingDetail from '@component/trade/buyer/myBidding/detail.vue';
import BuyerOrder from '@component/trade/buyer/order/index.vue';
import BuyerOrderDetail from '@component/trade/buyer/order/detail.vue';
import WantBill from '@component/trade/buyer/wantBill/index.vue';
import NewOffer from '@component/trade/buyer/wantBill/newOffer.vue';
import NewWantBill from '@component/trade/buyer/wantBill/newWantBill.vue';
import StockBill from '@/component/trade/buyer/stockBill/index.vue';
import StockBillDetail from '@/component/trade/buyer/stockBill/detail.vue';

 
// 我是卖方
// import Seller from '@component/trade/seller/index.vue';
import Posted from '@component/trade/seller/posted/index.vue';
import MyBill from '@component/trade/seller/myBill/index.vue';
import MyBillDetail from '@component/trade/seller/myBill/detail.vue';
import SellerOrder from '@component/trade/seller/order/index.vue';
import SellerOrderDetail from '@component/trade/seller/order/detail.vue';
// import EditBill from '@component/trade/seller/myBill/EditBill.vue';

const routes = [{
    path: '',
    redirect: { name: 'info' }
}, {
    path: '/seller',
    component: Main,
    meta: {
        tagName: '我是卖方'
    },
    children: [{
        path: 'posted/:checks/:id',
        name: 'posted',
        // props: true,
        component: Posted,
        meta: {
            tagName: '发布票据'
        }
    }, {
        path: 'myBill',
        name: 'myBill',
        component: MyBill,
        meta: {
            tagName: '我的票据'
        }
    }, {
        path: 'myBill/detail/:id',
        name: 'myBillDetail',
        component: MyBillDetail,
        props: true,
        meta: {
            tagName: '报价详情'
        }
    },{
        path: 'editBill/:checks/:id',
        name: 'editBill',
        component: Posted,
        props: true,
        meta: {
            tagName: '修改票据'
        }
    },
    {
        path: 'order',
        name: 'order',
        component: SellerOrder,
        meta: {
            tagName: '卖方订单'
        }
    },{
        path: 'order/detail/:id',
        name: 'SellerOrderDetail',
        component: SellerOrderDetail,
        props: true,
        meta: {
            tagName: '订单详情'
        }
    }]
}, {
    path: '/buyer',
    name: 'buyer',
    component: Main,
    meta: {
        tagName: '我是买方'
    },
    children: [{
            path: 'myBidding',
            name: 'myBidding',
            component: MyBidding,
            meta: {
                tagName: '我的竞价'
            }
        }, {
            path: 'myBidding/detail/:id',
            name: 'myBiddingDetail',
            component: MyBiddingDetail,
            props: true,
            meta: {
                tagName: '竞价详情'
            }
        }, {
            path: 'order',
            name: 'buyerOrder',
            component: BuyerOrder,
            meta: {
                tagName: '买方订单'
            }
        }, {
            path: 'order/detail/:id',
            name: 'buyerOrderDetail',
            component: BuyerOrderDetail,
            props: true,
            meta: {
                tagName: '订单详情'
            }
        }, {
            path: 'stockBill/:id',
            name: 'stockBill',
            component: StockBill,
            meta: {
                tagName: '库存票'
            }
        }, {
            path: 'stockBill/:id/:bill_id',
            name: 'stockBillDetail',
            component: StockBillDetail,
            meta: {
                tagName: '库存票详情'
            }
        }, {
            path: 'wantBill/:channel?',
            props: true,
            name: 'wantBill',
            component: WantBill,
            meta: {
                tagName: '发布买票'
            }
        }, {
            path: 'newOffer/:id?',
            props: true,
            name: 'newOffer',
            component: NewOffer,
            meta: {
                tagName: '新建行口报价'
            }
        }, {
            path: 'newWantBill',
            name: 'newWantBill',
            component: NewWantBill,
            meta: {
                tagName: '新建货款报价'
            }
        }
    ]
}, {
    path: '/enterprise',
    component: Main,
    meta: {
        tagName: '企业管理'
    },
    children: [{
        path: 'info',
        name: 'info',
        props: true,
        component: Info,
        meta: {
            tagName: '企业信息'
        }
    }, {
        path: 'judge',
        name: 'judge',
        component: Judge,
        meta: {
            tagName: '执剑人账户'
        }
    }, {
        path: 'draftBeans',
        name: 'draftBeans',
        component: DraftBeans,
        meta: {
            tagName: '汇票豆账户'
        }
    }, {
        path: 'safe',
        name: 'safe',
        component: Safe,
        meta: {
            tagName: '安全中心'
        }
    }, {
        path: 'message',
        name: 'message',
        component: Message,
        meta: {
            tagName: '消息中心'
        }
    }, {
        path: 'operater',
        name: 'operater',
        component: Operater,
        meta: {
            tagName: '操作员'
        }
    }]
}, {
    path: '/discount',
    component: Main,
    meta: {
        tagName: '银行速贴'
    },
    children: [
    {
        path: 'publish/:id?',
        name: 'publish',
        component: Publish,
        meta: {
            tagName: '发起速贴'
        }
    },
    {
        path: 'order',
        name: 'order',
        component: DiscountOrder,
        meta: {
            tagName: '速贴订单'
        }
    },{
            path: 'order/detail/:id',
            name: 'DiscountOrderDetail',
            component: DiscountOrderDetail,
            meta: {
                tagName: '速贴订单详情'
            }
        }]
}]

const router = new Router({
    mode: 'hash',
    scrollBehavior(to, from, savedPosition) {
        return { x: 0, y: 0 }
    },
    routes
});

router.beforeEach((to, from, next) => {
    if (!localStorage.getItem('userInfo')) {
        location.href = `user.html#/?back=${location.pathname.replace(/^\//, '')}${location.hash}`
        return false;
    }
    next()
    if(!state.tabs.some(tab => { return tab.name === to.path })) {
        store.commit('addTab', {
            title: `${to.matched[0].meta.tagName}-${to.meta.tagName}`,
            name: to.path
        })
    }
})

export {
    router
};